﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using Telerik.Web.UI;
using SMA.GENTEv1;

public partial class Protegidos_Empresas_Cadastros__wf_wfFP : System.Web.UI.Page
{
    protected int idWfConfig = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["IdWfConfig"] != null)
            idWfConfig = Int32.Parse(Request.QueryString["IdWfConfig"].ToString());

        if (!IsPostBack)
        {
            ExibeParametros();
        }
    }

    #region Eventos

    protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
    {
        //RadGrid1.MasterTableView.SortExpressions.Clear();
        //RadGrid1.MasterTableView.GroupByExpressions.Clear();
        //RadGrid1.MasterTableView.CurrentPageIndex = RadGrid1.MasterTableView.PageCount - 1;
        //RadGrid1.Rebind();
        if (e.Argument == "tpPagUnicoA")
        {
            CarregarPagUnicoAlerta(Convert.ToInt32(hdIdWfConfigData.Value));
        }
        else if (e.Argument == "tpPagUnicoA")
        {
            CarregarPagUnicoAviso(Convert.ToInt32(hdIdWfConfigData.Value));
        }
    }

    #region Pagamento Unico

    #region Alerta
    protected void GridAlerta_DeleteCommand(object source, GridCommandEventArgs e)
    {
        int id = int.Parse((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["IdWfConfigParametro"].ToString());

        WorkflowConfigParametro wfParametro = new WorkflowConfigParametro(id);
        wfParametro.Excluir();

        CarregarPagUnicoAlerta(Convert.ToInt32(hdIdWfConfigData.Value));
    }
    protected void GridAlerta_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        //if (e.CommandName == "Select")
        //{
        //    string link = string.Empty;
        //    int idProcessoContrato = Int32.Parse(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["IdProcessoContrato"].ToString());

        //    ProcessoContrato processo = new ProcessoContrato(idProcessoContrato);

        //    link = "_wf/wf" + processo.TipoProcesso.Url + "?idProcessoContrato=" + idProcessoContrato.ToString() + "&idContrato=" + rcbContrato.SelectedValue.ToString();

        //    Server.Transfer(link);
        //}
    }
    protected void GridAlerta_ItemCreated(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridCommandItem)
        {
            int total = 0;

            if (e.Item.OwnerTableView.DataSourceCount != null)
                total = e.Item.OwnerTableView.DataSourceCount;

            if (total >= 4)
            {
                HtmlGenericControl btnInsert = e.Item.FindControl("dvGridInsert") as HtmlGenericControl;
                btnInsert.Visible = false;
            }
        }
    }
    #endregion

    #region Pendencia
    protected void GridPendencia_DeleteCommand(object source, GridCommandEventArgs e)
    {
        int id = int.Parse((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["IdWfConfigParametro"].ToString());

        WorkflowConfigParametro wfParametro = new WorkflowConfigParametro(id);
        wfParametro.Excluir();

        CarregarPagUnicoAviso(Convert.ToInt32(hdIdWfConfigData.Value));
    }
    protected void GridPendencia_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        //if (e.CommandName == "Select")
        //{
        //    string link = string.Empty;
        //    int idProcessoContrato = Int32.Parse(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["IdProcessoContrato"].ToString());

        //    ProcessoContrato processo = new ProcessoContrato(idProcessoContrato);

        //    link = "_wf/wf" + processo.TipoProcesso.Url + "?idProcessoContrato=" + idProcessoContrato.ToString() + "&idContrato=" + rcbContrato.SelectedValue.ToString();

        //    Server.Transfer(link);
        //}
    }
    protected void GridPendencia_ItemCreated(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridCommandItem)
        {
            int total = 0;

            if (e.Item.OwnerTableView.DataSourceCount != null)
                total = e.Item.OwnerTableView.DataSourceCount;

            if (total >= 4)
            {
                HtmlGenericControl btnInsert = e.Item.FindControl("dvGridInsert") as HtmlGenericControl;
                btnInsert.Visible = false;
            }
        }
    }
    #endregion

    #endregion

    #region Adiantamento

    #region Alerta
    #endregion

    #region Pendencia
    #endregion

    #endregion

    #region Pagamento Principal

    #region Alerta
    #endregion

    #region Pendencia
    #endregion

    #endregion

    #region 1º Parcela - 13º Salário

    #region Alerta
    #endregion

    #region Pendencia
    #endregion

    #endregion

    #region 2º Parcela - 13º Salário

    #region Alerta
    #endregion

    #region Pendencia
    #endregion

    #endregion

    #endregion

    #region Metodos
    private void ExibeParametros()
    {
        //WorkflowConfig wfConfig = new WorkflowConfig(idWfConfig);
        //1	Pagamento Principal
        //2	Pagamento Adiantamento
        //3	Pagamento Unico
        //4	13° - Pagamento 1° Parcela
        //5	13° - Pagamento 2ª Parcela

        dvPagUnico.Visible = false;
        dvPagPrincipal.Visible = false;
        dvAdiantamento.Visible = false;
        dv113.Visible = false;
        dv213.Visible = false;

        WorkflowConfigData wfConfigData = new WorkflowConfigData();
        List<WorkflowConfigData> listWkConfigData = wfConfigData.SelectPor(idWfConfig);

        foreach (WorkflowConfigData item in listWkConfigData)
        {
            switch (item.TipoPagamento.IdTipoPagamento)
            {
                case 1:
                    dvPagPrincipal.Visible = true;
                    CarregarPagPrincipal(item.IdWfConfigData);
                    break;
                case 2:
                    dvAdiantamento.Visible = true;
                    CarregarAdiantamento(item.IdWfConfigData);
                    break;
                case 3:
                    dvPagUnico.Visible = true;
                    CarregarPagUnicoAlerta(item.IdWfConfigData);
                    CarregarPagUnicoAviso(item.IdWfConfigData);
                    hdIdWfConfigData.Value = item.IdWfConfigData.ToString();
                    break;
                case 4:
                    dv113.Visible = true;
                    Carregar113(item.IdWfConfigData);
                    break;
                case 5:
                    dv213.Visible = true;
                    Carregar213(item.IdWfConfigData);
                    break;
                default:
                    break;
            }
        }
    }
    private void CarregarPagUnicoAlerta(int idWfConfigData)
    {
        WorkflowConfigParametro wfParametro = new WorkflowConfigParametro();
        List<WorkflowConfigParametro> listParametrosAlerta = wfParametro.SelectAlerta(idWfConfigData, "Composto");

        GridAlerta.DataSource = listParametrosAlerta;
        GridAlerta.DataBind();
    }
    private void CarregarPagUnicoAviso(int idWfConfigData)
    {
        WorkflowConfigParametro wfParametro = new WorkflowConfigParametro();
        List<WorkflowConfigParametro> listParametrosAviso = wfParametro.SelectAviso(idWfConfigData, "Composto");

        GridPendencia.DataSource = listParametrosAviso;
        GridPendencia.DataBind();
    }


    private void CarregarPagPrincipal(int idWfConfigData)
    {
    }
    private void CarregarAdiantamento(int idWfConfigData)
    {
    }
    private void Carregar113(int idWfConfigData)
    {
    }
    private void Carregar213(int idWfConfigData)
    {
    }
    #endregion
    
}
